package cn.edu.imau.zy.system.controller;

import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.constant.UserConstants;
import cn.edu.imau.zy.common.core.controller.BaseController;
import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.entity.student.StStudent;
import cn.edu.imau.zy.common.core.entity.system.SysSignDetail;
import cn.edu.imau.zy.common.core.entity.system.SysSignJob;
import cn.edu.imau.zy.common.core.entity.system.SysUser;
import cn.edu.imau.zy.system.feign.RemoteStudentService;
import cn.edu.imau.zy.system.service.ISysSignDetailService;
import cn.edu.imau.zy.system.service.ISysSignJobService;
import cn.edu.imau.zy.system.service.ISysUserService;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 签到任务 提供者
 * 
 * @author wyz
 * @date 2020-07-07
 */
@RestController
@RequestMapping("signjob")
public class SysSignJobController extends BaseController
{
	
	@Autowired
	private ISysSignJobService sysSignJobService;
	@Autowired
	private ISysSignDetailService sysSignDetailService;
	
	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{id}")
	public SysSignJob get(@PathVariable("id") String id)
	{
		return sysSignJobService.selectSysSignJobById(id);
	}
	
	/**
	 * 查询签到任务列表
	 */
	@GetMapping("list")
	public R list(SysSignJob sysSignJob)
	{
		startPage();
        return result(sysSignJobService.selectSysSignJobList(sysSignJob));
	}
	
	
	/**
	 * 新增保存签到任务
	 */
	@PostMapping("save")
	public R addSave(@RequestBody SysSignJob sysSignJob,@LoginUser SysUser user)
	{
        sysSignJob.setCreateBy(user.getUserId());
        sysSignJob.setStatus("1");
		sysSignJob.setId(IdUtil.getSnowflake(1L,1L).nextIdStr());
        return toAjax(sysSignJobService.insertSysSignJob(sysSignJob));
    }

    /**
     * 开始任务
     */
    @GetMapping("start/{id}")
    public R start(@PathVariable("id") String id, @LoginUser SysUser user) {
        SysSignJob job = sysSignJobService.selectSysSignJobById(id);
        Map<String, Object> params = new HashMap<>();
        params.put("deptId", job.getDeptId());
        params.put("classesIds", job.getClassesIds());
        params.put("grades", job.getGrades());
        params.put("educationalLevels", job.getEducationalLevels());
        params.put("signJobId", job.getId());

        Map<String, Object> res = new HashMap<>();

        res.put("d", sysSignDetailService.deleteSysSignDetail(params));

        List<StStudent> students = sysSignJobService.selectStudents(params);
		List<SysSignDetail> insList = new ArrayList<>();
        for (StStudent student : students) {
			SysSignDetail signDetail = new SysSignDetail();
			signDetail.setId(IdUtil.getSnowflake(1L,1L).nextIdStr());
            signDetail.setSignJobId(job.getId());
			signDetail.setStatus(1L);
            signDetail.setCreateBy(user.getUserId());
			signDetail.setCreateTime(new Date());
            signDetail.setUserId(Long.valueOf(student.getStudentId()));
            signDetail.setUserName(student.getStudentName());
			insList.add(signDetail);
        }
        if (students.size() > 0 && insList.size() > 0) {
            res.put("i", sysSignDetailService.insertSysSignDetailList(insList));
        } else {
            res.put("i", 0);
        }
        job.setStatus("2");
        sysSignJobService.updateSysSignJob(job);
        return R.ok(res);
	}

	/**
	 * 修改保存签到任务
	 */
	@PostMapping("update")
	public R editSave(@RequestBody SysSignJob sysSignJob,@LoginUser SysUser user) {
        sysSignJob.setUpdateBy(user.getUserId());
		return toAjax(sysSignJobService.updateSysSignJob(sysSignJob));
	}
	
	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{
		sysSignDetailService.deleteSysSignDetailByJobIds(ids);
		return toAjax(sysSignJobService.deleteSysSignJobByIds(ids));
    }


    /**
     * 签到任务汇总统计
     */
    @GetMapping("count")
    public R count() {
        return R.data(sysSignJobService.count());
	}
}
